package com.halflife.comeElectricity.member.rider;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.pay.wxpay.WXPayAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class RefundLogAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RefundLogRepository refundLogRepository;
    @Resource
    private PayLogRepository payLogRepository;

    @RequestMapping("/refundLog/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Integer payLogId,Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        PayLog payLog = payLogRepository.selectByPrimaryKey(payLogId);
        page.setQueryParam(queryParam);
        int totalCount = refundLogRepository.selectCount(queryParam);
        List<RefundLog> list = refundLogRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        model.addAttribute("payLogId",payLogId);
        model.addAttribute("payLog",payLog);
        return "refundLog/list";
    }

    @RequestMapping("/refundLog/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        PayLog payLog = payLogRepository.selectByPrimaryKey(id);

        model.addAttribute("payLog",payLog);
        return "refundLog/operate";
    }

    @PostMapping("/refundLog/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(RefundLog refundLog, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            refundLog.init();
            PayLog payLog = payLogRepository.selectByPrimaryKey(refundLog.getPayLogId());
            refundLog.setRiderId(payLog.getRiderId());
            refundLog.setCatagory("微信退款");
            BigDecimal refundedMoney = payLog.getRefundMoney()==null?BigDecimal.ZERO:payLog.getRefundMoney();
            BigDecimal spareMoney = payLog.getPayMoney().subtract(refundedMoney);
            if (refundLog.getRefundMoney().compareTo(spareMoney) > 0) {
                log.error("退款金额小于剩余金额:"+payLog.getId());
                return new ResponseEntity("退款金额小于剩余金额", HttpStatus.INTERNAL_SERVER_ERROR);
            }
            Integer a = refundLogRepository.insert(refundLog);
            Map map = null;
            WXPayAgent agent = new WXPayAgent(payLog.getId().toString(), payLog.getTransaction_id(),
                    payLog.getPayMoney().multiply(new BigDecimal("100")).intValue(),
                    refundLog.getRefundMoney().multiply(new BigDecimal("100")).intValue(),
                    refundLog.getId().toString(),"退款");
            String result = agent.doRefund("");
            log.debug("退款调用:"+result);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/refundLog/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            RefundLog refundLog = new RefundLog();
            refundLog.setId(id);
            refundLog.setValid(Boolean.FALSE);
            refundLogRepository.updateByPrimaryKeySelective(refundLog);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
